package com.bincloud.vue.admin.web.bo;

import com.bincloud.vue.admin.model.dto.ResourceDTO;
import com.bincloud.vue.admin.model.dto.UserDTO;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 管理用户业务封装体
 * @author bincloud, mvpzhou
 */
public class VueAdminUserDetails implements UserDetails {

    /**
     * 用户信息
     */
    private final UserDTO user;
    /**
     * 权限列表
     */
    private final Set<ResourceDTO> resSet;

    public VueAdminUserDetails(UserDTO user, Set<ResourceDTO> resSet) {
        this.user = user;
        this.resSet = resSet;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 返回权限名称列表
        // 权限代码 模块:资源
        // 例如：user:page
        return resSet.stream()
                .map(authority -> new VueAdminGrantedAuthority(authority.getCode()))
                .collect(Collectors.toSet());
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return user.isAccountNonExpired();
    }

    @Override
    public boolean isAccountNonLocked() {
        return user.isAccountNonLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return user.isCredentialsNonExpired();
    }

    @Override
    public boolean isEnabled() {
        return user.isEnabled();
    }
}
